import BuffUtil from '@/BuffUtil';
import MathUtil from '@/MathUtil';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `${ColorSafe}900|r范围内有单位死亡时增加${ColorSafe}4|r点攻击力，持续${ColorSafe}80|r秒。觉醒效果：持续时间增加至${ColorSafe}120秒`;
const art = 'ReplaceableTextures\\CommandButtons\\BTNWandSkull.blp';
const name = '亡者之遗';
const config: TalentConfigModel = {
    id: 'talent_t_TalentInheritanceOfGhost',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_TalentInheritanceOfGhost',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_TalentInheritanceOfGhost_plus',
        art,
        name: '亡者之遗[觉醒]',
        ubertip: `${ColorSafe}900|r范围内有单位死亡时增加${ColorSafe}4|r点攻击力，持续${ColorSafe}120|r秒`,
    },
};

const effectBuffType: AppBuffType = {
    id: 'buff_t_TalentInheritanceOfGhost_effect',
    art,
    name: '亡者之遗',
    ubertip: `攻击力得到加强`,
    stackDurMode: true,
    stackCountMax: -1,
    attribute: {
        attack: 4,
    },
    dur: 80,
};

const effectBuffTypePlus: AppBuffType = {
    id: 'buff_t_TalentInheritanceOfGhostPlus_effect',
    art,
    name: '亡者之遗',
    ubertip: `攻击力得到加强`,
    stackDurMode: true,
    stackCountMax: -1,
    attribute: {
        attack: 4,
    },
    dur: 120,
};

export default class TalentInheritanceOfGhost extends TalentBase {
    static instance: TalentInheritanceOfGhost;
    private _casters: unit[] = [];
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentInheritanceOfGhost.instance = this;
        this.setTalentConfig(config);
        this.init();
        this.onAddTalent(this.addTalentEffect);
        this.onAddTalentPlus(this.addTalentPlusEffect);
        EventManager.单位死亡(this.deathEffect);
    }
    addTalentEffect(u: unit) {
        let _this = TalentInheritanceOfGhost.instance;
        if (_this._casters.includes(u)) return;
        _this._casters.push(u);
    }
    addTalentPlusEffect(u: unit) {
        let prevbuff = BuffUtil.getUnitBuff(u, effectBuffType.id);
        if (prevbuff == null) return;
        let newBuff = BuffUtil.addBuff(u, effectBuffTypePlus.id, u);
        newBuff.stackCount = prevbuff.stackCount;
    }
    init() {
        BuffUtil.registerBuffType(effectBuffType);
        BuffUtil.registerBuffType(effectBuffTypePlus);
    }
    deathEffect(d: DamageEventData) {
        let _this = TalentInheritanceOfGhost.instance;
        for (let c of _this._casters) {
            let dis = MathUtil.distanceBetweenUnits(d.target, c);
            if (dis > 900) continue;
            let { unEffect, plus } = _this.talentUnEffect(c);
            let id = plus ? effectBuffTypePlus.id : effectBuffType.id;
            BuffUtil.addBuff(c, id, c);
        }
    }
}
